package com.mytaobao.main.firsttab.func.chaoshihui;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.imageload.ImageLoaderUtil;
import com.baseproject.json.JsonUtils;
import com.mytaobao.R;
import com.mytaobao.main.firsttab.view.ItemViewGroupBase;
import com.mytaobao.main.firsttab.view.MyImageView;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.structure.view.ITangramViewLifeCycle;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 超实惠--天猫必逛--特色好货
 */
public class ItemChaoShiHuiView extends ItemViewGroupBase implements ITangramViewLifeCycle{

    @BindView(R.id.title)
    TextView tvTitle;
    @BindView(R.id.title_icon)
    ImageView ivTitleIcon;

    String title;
    int titleIconRid;

    ArrayList<ArrayList<Float>> imageWeights = new ArrayList<ArrayList<Float>>();

    public ItemChaoShiHuiView(Context context) {
        this(context, null);
    }

    public ItemChaoShiHuiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, R.layout.item_chao_shi_hui);
        ButterKnife.bind(this);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemChaoShiHuiView);
        if (typedArray != null) {
            titleIconRid = R.drawable.chaoshihui_icon;
            title = "超实惠";
            String[] weights = "1.6,0.8,0.8,0.8;1.6,0.8,0.8,0.8".split(";");
            for (int i = 0 ; i < weights.length; i++) {
                String cur = weights[i];
                String[] row = cur.split(",");
                ArrayList<Float> rowWeight = new ArrayList<>();
                for(int j = 0 ; j < row.length; j++) {
                    rowWeight.add(Float.parseFloat(row[j]));
                }
                imageWeights.add(rowWeight);
            }
            typedArray.recycle();
        }
        setTitle();
        initLayout();
    }

    public ItemChaoShiHuiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, R.layout.item_chao_shi_hui);
    }

    @Override
    public void cellInited(BaseCell cell) {

    }

    @Override
    public void postBindView(BaseCell cell) {
        JSONArray jsonArray = cell.optJsonArrayParam("imageUrls");
        setData(JsonUtils.getInstance().JSONArrayToList(jsonArray, String.class));
    }

    @Override
    public void postUnBindView(BaseCell cell) {

    }

    public void setTitle() {
        ivTitleIcon.setScaleType(ImageView.ScaleType.FIT_XY);
        ivTitleIcon.setImageResource(titleIconRid);
        tvTitle.setText(title);
    }

    public void initLayout() {
            for (int i = 0; i < imageWeights.size(); i++) {
                ArrayList<Float> currentWeight = imageWeights.get(i);
                addHorizontalLine(this);
                LinearLayout llRow = addLinearLayout(this, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0, 0, HORIZONTAL);
                for (int j = 0; j < currentWeight.size(); j++) {
                    addImageView(imageWeights, i, llRow, j);
                    //垂直分割线
                    if (j != currentWeight.size() - 1) {
                        addVerticalLine(llRow);
                    }
                }
            }
            addDivider(this, 10);
    }

    boolean haveSetData =  false;
    public void setData(List<String> itemChaoShiHui) {
        if(!haveSetData) {
            Iterator<String> iter = itemChaoShiHui.iterator();
            for (int i = 1; i < this.getChildCount(); i++) {
                View child = this.getChildAt(i);
                if (child instanceof LinearLayout) {
                    LinearLayout llChild = (LinearLayout)child;
                    for (int j = 0; j < llChild.getChildCount(); j++){
                        View subChild = llChild.getChildAt(j);
                        if (subChild instanceof ImageView) {
                            ImageLoaderUtil.getInstance().with(mContext)
                                    .load(iter.next())
                                    .into((ImageView)subChild);
                        }
                    }
                }
            }
            haveSetData = true;
        }
    }

    private void addImageView(ArrayList<ArrayList<Float>> imageWeights, int i, LinearLayout parent, int j) {
        //设置image
        LayoutParams lpImage = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
        lpImage.weight = imageWeights.get(i).get(j);

        //自定义高宽比，期间调试sdk发现需要设置父LL的setBaselineAligned为false，才能在onMeasure时拿到正确的width
        ImageView iv = new MyImageView(mContext, (float) 1.2 / lpImage.weight);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        parent.addView(iv, lpImage);
    }

}
